

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store.Web.Controls;
using JTC.MiniCommerce.Store;
using JTC.MiniCommerce.Store.Services.PaymentService;
using SubSonic.Utilities;

namespace JTC.MiniCommerce.Web.admin.controls.order {
  public partial class actions : AdminControl {
    
    #region Member Variables
    
    private int orderId = 0;
    private string view = string.Empty;
    
    #endregion
    
    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        orderId = Utility.GetIntParameter("orderId");
        view = Utility.GetParameter("view");
        if(orderId > 0 && view == "a") {
          SetActionsProperties();
          PaymentService paymentService = new PaymentService();
          Order order = new Order(orderId);
          if(paymentService.PaymentServiceSettings.DefaultProvider == "PayPalStandardPaymentProvider" || order.OrderStatusId == (int)OrderStatus.OrderRefunded) {
            btnRefundTransaction.Visible = false;
          }
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(actions).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnRefundTransaction control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnRefundTransaction_Click(object sender, EventArgs e) {
      try {
        Transaction transaction = new TransactionController().FetchByOrderIdAndTransactionTypeId(orderId, (int)TransactionType.Charge);
        OrderController.Refund(transaction, WebUtility.GetUserName());
        base.MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblOrderRefunded"));
      }
      catch(Exception ex) {
        Logger.Error(typeof(actions).Name + ".btnRefundTransaction_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }    
    
    #endregion
    
    #region Methods
    
    #region Private

    private void SetActionsProperties() {
      LocalizationUtility.AddButtonText(btnRefundTransaction);
    }
    
    #endregion
    
    #endregion
    
  }
}